package com.bwda.dsrs.filecenter.service.impl;

import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.po.terminalblacklist.TerminalBlackListPo;
import com.bwda.dsrs.filecenter.domain.condition.terminalblacklist.TerminalBlackListCondition;
import com.bwda.dsrs.filecenter.mapper.dsrs.TerminalBlackListMapper;
import com.bwda.dsrs.filecenter.service.TerminalBlackListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 *
 * @author system
 * @date 2019/4/26
 */
@Service
public class TerminalBlackListServiceImpl extends BaseServiceImpl<TerminalBlackListPo, TerminalBlackListCondition, TerminalBlackListMapper> implements TerminalBlackListService {

	@Autowired
	private  TerminalBlackListMapper terminalBlackListMapper;

	/**
	 *  将字符串形式IP地址转换long类型
	 * @param ip
	 * @return
	 */
	public static long getIp2long(String ip) {
		ip = ip.trim();
		String[] ips = ip.split("\\.");
		long ip1 = Integer.parseInt(ips[0]);
		long ip2 = Integer.parseInt(ips[1]);
		long ip3 = Integer.parseInt(ips[2]);
		long ip4 = Integer.parseInt(ips[3]);
		long ip2long =1L* ip1 * 256 * 256 * 256 + ip2 * 256 * 256 + ip3 * 256 + ip4;
		return ip2long;
	}

	/**
	 * 判断一个ip地址是否在某个ip段范围内
	 * @param ip
	 * @param startIp
	 * @param endIp
	 * @return
	 */
	@Override
	public  boolean ipExistsInRange(String ip, String startIp, String endIp) {
		return (getIp2long(startIp)<=getIp2long(ip)) && (getIp2long(ip)<=getIp2long(endIp));
	}


	@Override
	public void batchDelete(List<Integer> id){
		TerminalBlackListPo po  = new TerminalBlackListPo();
		po.setDelFlag(BusinessConstants.NumberConstant.ONE);
		po.setIdList(id);
		this.getMapper().update(po);
	}
}